﻿namespace MF.HSD.StdUnitCostBP
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using UFIDA.U9.CA.BaseData;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;
    using UFSoft.UBF.Util.DataAccess;

    /// <summary>
    /// CreateStdUnitCost partial 
    /// </summary>	
    public partial class CreateStdUnitCost
    {
        internal BaseStrategy Select()
        {
            return new CreateStdUnitCostImpementStrategy();
        }
    }

    #region  implement strategy	
    /// <summary>
    /// Impement Implement
    /// 
    /// </summary>	
    internal partial class CreateStdUnitCostImpementStrategy : BaseStrategy
    {
        public CreateStdUnitCostImpementStrategy() { }

        public override object Do(object obj)
        {
            CreateStdUnitCost bpObj = (CreateStdUnitCost)obj;

            string result = "";
            string sql = @"select	A.ItemInfo_ItemCode as [ItemInfo_ItemCode], A.ItemInfo_ItemName as [ItemInfo_ItemName],CostFieldCode,Currency,
			sum(A.Temp_BalMny) as [Temp_BalMny], sum(A.Cost_BalCollabAmt) as 本层外协费, 
			sum(A.Cost_BalItemAmt) as 本层材料费, sum(A.Cost_BalMFCAmt) as 本层制造费, sum(A.Cost_BalMacAmt) as 本层机器费, 
			sum(A.Cost_BalPurAmt) as 本层采购成本, sum(A.Cost_BalWorkAmt) as 本层人工费, sum(A.Cost_BalNextCollabAmt) as 下层外协费, 
			sum(A.Cost_BalNextItemAmt) as 下层材料费, sum(A.Cost_BalNextMFCAmt) as 下层制造费, sum(A.Cost_BalNextMacAmt) as 下层机器费, 
			sum(A.Cost_BalNextPurAmt) as 下层采购成本, sum(A.Cost_BalNextWorkAmt) as 下层人工费 
			from  ( select  B.[ItemInfo_ItemCode] as [ItemInfo_ItemCode] ,B4.Code CostFieldCode,B3.Currency
			,case  when B2.[ItemFormAttribute] in (16, 22) then B.[ItemInfo_ItemName] else B2.[Name] end  as [ItemInfo_ItemName], 
			sum(B3.[CostBalMny]) as [Temp_BalMny], sum(B3.[BalCollabAmt]) as [Cost_BalCollabAmt], sum(B3.[BalItemAmt]) as [Cost_BalItemAmt], sum(B3.[BalMFCAmt]) as [Cost_BalMFCAmt], 
			sum(B3.[BalMacAmt]) as [Cost_BalMacAmt], sum(B3.[BalPurAmt]) as [Cost_BalPurAmt], sum(B3.[BalWorkAmt]) as [Cost_BalWorkAmt], sum(B3.[BalNextCollabAmt]) as [Cost_BalNextCollabAmt],
			sum(B3.[BalNextItemAmt]) as [Cost_BalNextItemAmt], sum(B3.[BalNextMFCAmt]) as [Cost_BalNextMFCAmt], sum(B3.[BalNextMacAmt]) as [Cost_BalNextMacAmt], 
			sum(B3.[BalNextPurAmt]) as [Cost_BalNextPurAmt], sum(B3.[BalNextWorkAmt]) as [Cost_BalNextWorkAmt] 
			from InvTrans_AccountPeriodLine as B  
				left join [CBO_ItemMaster] as B2 on (B.[ItemInfo_ItemID] = B2.[ID])  
				left join [InvTrans_AccountPeriodLineCost] as B3 on (B.[ID] = B3.[AccountPeriodLine])  
				left join [CBO_CostField] as B4 on (B3.[CostField] = B4.[ID])  
				left join [CBO_CostType] as B5 on (B3.[CostType] = B5.[ID])  
				left join [Base_Organization] as B8 on (B.[Org] = B8.[ID])  
			where  B3.[IsMainSOB] = 1
			and B3.[IsAccCostType] = 1 and B.[QtyPriceDealFlg] in (0, 2, 4) and B8.[Code] = '" + bpObj.OrgCode + @"' and B.[AccountPeriod] in(select ID from Base_AccountingPeriod where DisplayName='" + bpObj.DisPlayName + @"')  and B4.Code='" + bpObj.CostField + @"'
			and B.ItemInfo_ItemID in (select distinct a.ItemMaster From CA_OutputQty a
				inner join Base_Organization org on a.Org = org.ID
				inner join CBO_ItemMaster item on a.ItemMaster = item.ID
				inner join CBO_CostField cf on cf.ID = a.CostField
				where org.Code = '" + bpObj.OrgCode + @"' and a.AccountingPeriod in(select ID from Base_AccountingPeriod where DisplayName='" + bpObj.DisPlayName + @"') 
				and cf.Code = '" + bpObj.CostField + @"')
			group by B.[ItemInfo_ItemCode],  case  when B2.[ItemFormAttribute] in (16, 22) then B.[ItemInfo_ItemName] else B2.[Name] end ,B4.Code,Currency
			) as A 
			group by A.ItemInfo_ItemCode, A.ItemInfo_ItemName,CostFieldCode,Currency
			having  ((sum(A.[Temp_BalMny]) != 0) or 
			(sum(A.[Cost_BalCollabAmt]) != 0) or 
			(sum(A.[Cost_BalItemAmt]) != 0) or 
			(sum(A.[Cost_BalMFCAmt]) != 0) or 
			(sum(A.[Cost_BalMacAmt]) != 0) or 
			(sum(A.[Cost_BalPurAmt]) != 0) or 
			(sum(A.[Cost_BalWorkAmt]) != 0) or 
			(sum(A.[Cost_BalNextCollabAmt]) != 0) or 
			(sum(A.[Cost_BalNextItemAmt]) != 0) or 
			(sum(A.[Cost_BalNextMFCAmt]) != 0) or 
			(sum(A.[Cost_BalNextMacAmt]) != 0) or 
			(sum(A.[Cost_BalNextPurAmt]) != 0) or 
			(sum(A.[Cost_BalNextWorkAmt]) != 0))
			order by A.[ItemInfo_ItemName] asc 	";
            DataSet ds = null;
            DataAccessor.RunSQL(DataAccessor.GetConn(), sql.ToString(), null, out ds);
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                using (Session se = Session.Open())
                {
                    //创建定额成本
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        StdUnitCost stdUnitCost = StdUnitCost.Create();
                        stdUnitCost.FromDate = DateTime.Today;
                        stdUnitCost.ToDate = DateTime.Parse("9999-12-31");
                        stdUnitCost.StdUnitCostType = UFIDA.U9.CA.Enums.StdUnitCostType_Enum.Normal;
                        stdUnitCost.Currency = UFIDA.U9.Base.Currency.Currency.Finder.FindByID(dr["Currency"]);
                        stdUnitCost.ItemMaster = UFIDA.U9.CBO.SCM.Item.ItemMaster.Finder.Find("Code='"+ dr["ItemInfo_ItemCode"].ToString() + "' and Org.Code='" + bpObj.OrgCode + "'");
                        stdUnitCost.Org = UFIDA.U9.Base.Organization.Organization.FindByCode(bpObj.OrgCode);
                        UFIDA.U9.Base.SOB.SetofBooks sobkey = UFIDA.U9.Base.SOB.SetofBooks.Finder.Find("Org.Code = '" + bpObj.OrgCode + "' and SOBType =0");
                        stdUnitCost.SOB = sobkey;
                        stdUnitCost.UnitCost = new UFIDA.U9.CBO.MFG.CO.ElementTypeCost();
                        //材料成本
                        stdUnitCost.UnitCost.MaterialCurrentCost = decimal.Parse(dr["本层材料费"].ToString()) + decimal.Parse(dr["本层采购成本"].ToString()); 
                        //材料下阶成本
                        stdUnitCost.UnitCost.MaterialPriorCost = decimal.Parse(dr["下层材料费"].ToString()) + decimal.Parse(dr["下层采购成本"].ToString());
                        //人工成本
                        stdUnitCost.UnitCost.LaborCurrentCost = decimal.Parse(dr["本层人工费"].ToString());
                        //人工下阶成本
                        stdUnitCost.UnitCost.LaborPriorCost = decimal.Parse(dr["下层人工费"].ToString());
                        //制造成本
                        stdUnitCost.UnitCost.OverheadCurrentCost = decimal.Parse(dr["本层制造费"].ToString());
                        //制造下阶成本
                        stdUnitCost.UnitCost.OverheadPriorCost = decimal.Parse(dr["下层制造费"].ToString());
                        //外协成本
                        stdUnitCost.UnitCost.SubcontractCurrentCost = decimal.Parse(dr["本层外协费"].ToString());
                        //外协下阶成本
                        stdUnitCost.UnitCost.SubcontractPriorCost = decimal.Parse(dr["下层外协费"].ToString());
                        //机器成本
                        stdUnitCost.UnitCost.MachineCurrentCost = decimal.Parse(dr["本层机器费"].ToString());
                        //机器下阶成本
                        stdUnitCost.UnitCost.MachinePriorCost = decimal.Parse(dr["下层机器费"].ToString());
                        se.Create(stdUnitCost);
                    }
                    se.Commit();
                }

            }
            return result;
        }
    }

    #endregion


}